package com.cecsmsserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.entity.NewsComment;
import com.cecsmsserve.entity.User;
import com.cecsmsserve.mapper.NewsCommentMapper;
import com.cecsmsserve.mapper.UserMapper;
import com.cecsmsserve.service.NewsCommentService;
import com.cecsmsserve.util.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NewsCommentServiceImpl extends ServiceImpl<NewsCommentMapper, NewsComment> implements NewsCommentService {

    @Autowired
    private NewsCommentMapper commentMapper;
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public CommonResult addComment(NewsComment comment) {
        // 设置评论时间
        comment.setCreateTime(LocalDateTime.now());
        // 设置状态为正常
        comment.setStatus(1);
        
        boolean saved = save(comment);
        if (!saved) {
            return CommonResult.failed("添加评论失败");
        }
        
        return CommonResult.success(comment.getId());
    }

    @Override
    public CommonResult deleteComment(Integer id, Integer userId) {
        // 检查评论是否存在且属于该用户
        LambdaQueryWrapper<NewsComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsComment::getId, id)
                .eq(NewsComment::getUserId, userId);
        
        NewsComment comment = getOne(wrapper);
        if (comment == null) {
            return CommonResult.failed("评论不存在或您无权删除");
        }
        
        // 逻辑删除，将状态设为0
        comment.setStatus(0);
        boolean updated = updateById(comment);
        if (!updated) {
            return CommonResult.failed("删除评论失败");
        }
        
        return CommonResult.success(null);
    }

    @Override
    public CommonResult adminDeleteComment(Integer id) {
        // 管理员可以直接删除任何评论
        NewsComment comment = getById(id);
        if (comment == null) {
            return CommonResult.failed("评论不存在");
        }
        
        // 逻辑删除，将状态设为0
        comment.setStatus(0);
        boolean updated = updateById(comment);
        if (!updated) {
            return CommonResult.failed("删除评论失败");
        }
        
        return CommonResult.success(null);
    }

    @Override
    public CommonResult getNewsComments(Integer newsId, Integer pageNum, Integer pageSize) {
        Page<NewsComment> page = new Page<>(pageNum, pageSize);
        
        LambdaQueryWrapper<NewsComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsComment::getNewsId, newsId)
                .eq(NewsComment::getStatus, 1) // 只获取状态为正常的评论
                .orderByDesc(NewsComment::getCreateTime);
        
        Page<NewsComment> commentPage = page(page, wrapper);
        
        // 填充用户信息
        List<NewsComment> comments = commentPage.getRecords();
        for (NewsComment comment : comments) {
            User user = userMapper.selectById(comment.getUserId());
            if (user != null) {
                comment.setUserName(user.getUsername());
                // User类没有avatar字段，暂时使用用户名作为用户标识
                comment.setUserAvatar(null);
            }
        }
        
        return CommonResult.success(commentPage);
    }

    @Override
    public CommonResult getUserComments(Integer userId, Integer pageNum, Integer pageSize) {
        Page<NewsComment> page = new Page<>(pageNum, pageSize);
        
        LambdaQueryWrapper<NewsComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsComment::getUserId, userId)
                .eq(NewsComment::getStatus, 1) // 只获取状态为正常的评论
                .orderByDesc(NewsComment::getCreateTime);
        
        Page<NewsComment> commentPage = page(page, wrapper);
        
        return CommonResult.success(commentPage);
    }
} 